//
//  TransferPage.swift
//  LeGou
//
//  Created by lxm on 2022/10/12.
//

import UIKit

class TransferPage: ZMViewController, UITableViewDelegate, UITableViewDataSource, ListRequestObjct {

    //MARK:--------------------属性--------------------
    var listData = [TransferInput.UserModel]()
    var page : Int = 1
    var pageSize : Int = 10
    var refreshScrollView : UIScrollView {
        tableView
    }
    //  类型[1个人 2商城]
    var type = 1
    //MARK:--------------------UI控件--------------------
    
    @IBOutlet weak var topContent: UIView!
    @IBOutlet weak var tableView: UITableView!
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        loadList()
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        ZMUtil.clipCircle(view: topContent, Corner: [.topLeft, .topRight], radius: 12)
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = "转账"
        tableView.zm.register(nibName: "GiveSearchUserCell", reuserId: "GiveSearchUserCell")
        tableView.delegate = self
        tableView.dataSource = self
        addRefresh(scrollView: tableView) { [unowned self] refresh in
            if refresh {
                self.page = 1
            }
            self.loadList()
        }
        
        addNodataScrollView(scrollView: tableView)
        emptyTitle = "暂无记录"
    }
    
    func addConstraints() {
        
    }
        
    //MARK:--------------------methods--------------------
    func bindAction() {
        ZMNotification.shard.subject.subscribe(onNext:{ [unowned self] resp in
            switch resp {
                case .updateVIPMallBankInfo, .updateBalance:
                    self.tableView.mj_header?.beginRefreshing()
                default:
                    break
            }
        }).disposed(by: disposeBag)
    }
    
    func loadList() {
        let pa = ["type" : type, "limit" : pageSize, "page" : page] as [String : Any]
        ZMUtil.listRequest(type: TransferInput.UserModel.self, api: MineAPI.transferHistory(pa)) { resp in
            self.listRequest(resp)
        }
    }
    
    @IBAction func onTransfer(_ sender: Any) {
        let vc = TransferInput()
        vc.type = type
        self.navigationController?.pushViewController(vc, animated: true)
    }
    

    @IBAction func onRecord(_ sender: Any) {
        let vc = TransFerRecord()
        vc.type = type
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    //MARK:--------------------delegate--------------------
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.mj_footer?.isHidden = listData.count == 0
        return listData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "GiveSearchUserCell") as! GiveSearchUserCell
        let data = listData[indexPath.row]
        cell.avatar.zm.setImage(urlStr: data.avatar)
        cell.nickname.text = data.nickname
        cell.histroy.text = "手机号：" + data.phone.zm.hidPhone()
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let data = listData[indexPath.row]
        let vc = TransferInput()
        vc.type = type
        vc.currentUser.accept(data)
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 64
    }
}
